﻿#include "addplatform.h"
#include "ui_addplatform.h"

AddPlatform::AddPlatform(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AddPlatform)
{
    ui->setupUi(this);
    this->setWindowTitle("AddPlatform");
    qDebug() << "AddPlatform::AddPlatform ---> construct begin.";
    P_ins = ParsexmlIns::getInstance();
    TypetoInt_Map = P_ins->getEventMap();
    qDebug() << "AddPlatform::AddPlatform ---> construct begin.";

//    std::thread t([this](){
//            open();
//    });
//    t.detach();
//            qDebug() << jsonData;
}

AddPlatform::~AddPlatform()
{
    delete ui;
}

void AddPlatform::fillJsonsToPlainText()
{
    Jsons = P_ins->getJsons();
    for(auto &json : Jsons)
        ui->plainTextEdit->appendPlainText(json);
//        qDebug() << json;
}

void AddPlatform::start()
{
    for(auto &json : Jsons){
//        qDebug() << "AddPlatform::start ---> begin run.";
        doc = QJsonDocument::fromJson(json.toUtf8(), &error);
        if (error.error != QJsonParseError::NoError) {
            qDebug() << error.errorString();
            return;
        }
        auto obj = doc.object();

        for(auto it = obj.begin(); it!= obj.end(); ++it){
            auto key = it.key();
            Eventtype = TypetoInt_Map[key];
            auto value = it.value();
            if(!value.isObject()){
                return;
            }
            auto obj = value.toObject();
            for(auto iter = obj.begin(); iter != obj.end(); ++iter){
                auto key = iter.key();
                auto value = iter.value();
                if(!value.isObject()){
                    auto strList = value.toString().split(",");
                    Res_Map[Eventtype] = strList;
                    P_ins->setRes_Map(Res_Map);
                }
                else{
                    _name = key;
                    //                qDebug() << "_name = " << _name;
                    auto obj = value.toObject();
                    if(_name != "others"){
                        P_ins->setname(_name);
                        for(auto iter = obj.begin(); iter != obj.end(); ++iter){
                            auto key = iter.key();
                            auto value =iter.value();
                            if(!value.isObject()){
                                TemPath_Map[_name] = value.toString();
                                P_ins->setTemPath_Map(TemPath_Map);
                            }
                            else{
                                auto obj = value.toObject();
                                Root_map[_name] = obj["_label_path"].toString().split(",");
                                P_ins->setRoot_map(Root_map);
                                auto operatename = obj["_insert_type"].toString();
                                setOperateType(operatename);
                            }
                        }
                    }
                    else{
                        for(auto iter = obj.begin(); iter != obj.end(); ++iter){
                            _name = iter.key();
                            P_ins->setname(_name);
                            auto value = iter.value();
                            if(value.isObject()){
                                auto obj = value.toObject();
                                Data_Map.clear();
                                for(auto iter = obj.begin(); iter != obj.end(); ++iter){
                                    auto key = iter.key();
                                    auto value =iter.value();
                                    if(!value.isObject()){
                                        auto operatename = obj["_insert_type"].toString();
                                        setOperateType(operatename);
                                    }else{
                                        auto obj = value.toObject();
                                        for(auto iter = obj.begin(); iter != obj.end(); ++iter){
                                            const QString &key = iter.key();
                                            page = key.split("-")[1].toInt();
                                            auto value = iter.value();
                                            if(value.isObject()){
                                                auto obj = value.toObject();
                                                for(auto iter = obj.begin(); iter != obj.end(); ++iter){
                                                    const QString &key = iter.key();
                                                    const QString &value = iter.value().toString();
                                                    auto strList = value.split(",");
                                                    m_Map[key] = strList;
                                                }
                                                Data_Map[page] = m_Map;
                                            }
                                        }
                                    }
                                }
                            }
                            selectSignal();
                            Operatetype = OperateType::None;
                            qDebug() << "==================";
                        }
                        Operatetype = OperateType::None;
                    }
                }
                //            qDebug() << "Operatetype = " << Operatetype;
                selectSignal();
                Operatetype = OperateType::None;
                qDebug() << "==================";
            }
        }
//        qDebug() << "AddPlatform::start ---> end run.";
    }
}

//void AddPlatform::open()
//{
//    openJsonFile(jsonData, error, doc, JSON_PATH);
//}

void AddPlatform::setOperateType(const QString &operatename)
{
    if(operatename == "append"){
        Operatetype = OperateType::Add;
    }else if(operatename == "replace"){
        Operatetype = OperateType::Mod;
    }else{
        Operatetype = OperateType::Remove;
    }

}

void AddPlatform::selectSignal()
{
    if(Operatetype == OperateType::Add){
        //        qDebug() << "will add event.";
        switch (Eventtype) {
        case addType::EVENT:
        {
            EventAdd *ev = new EventAdd;
            emit sendAddSignal(/*checkVec,*/ (AddEv*)ev);
            if(ev){
                delete ev;
            }
        }
        break;
        case addType::Parm:
        {
            ParmAdd *par = new ParmAdd;
            emit sendAddSignal(/*checkVec,*/ (AddEv*)par);
            if(par){
                delete par;
            }
        }
        break;
        case addType::Agent:
        {
            AgentAdd *agent = new AgentAdd;
            emit sendAddSignal(/*checkVec,*/ (AddEv*)agent);
            if(agent){
                delete agent;
            }
        }
        break;
        default:
            break;
        }
    }

    if(Operatetype == OperateType::Mod){
        switch (Eventtype) {
        case addType::EVENT:
        {
            EventAdd *ev = new EventAdd;
            emit sendModSignal(/*checkVec,*/ (AddEv*)ev, Data_Map);
            if(ev){
                delete ev;
            }
        }
        break;
        case addType::Parm:
        {
            ParmAdd *par = new ParmAdd;
            emit sendModSignal(/*checkVec,*/ (AddEv*)par, Data_Map);
            if(par){
                delete par;
            }
        }
        break;
        case addType::Agent:
        {
            AgentAdd *agent = new AgentAdd;
            emit sendModSignal(/*checkVec,*/ (AddEv*)agent, Data_Map);
            if(agent){
                delete agent;
            }
        }
        break;
        default:
            break;
        }
    }

    if(Operatetype == OperateType::Remove){
        switch (Eventtype) {
        case addType::EVENT:
        {
            EventAdd *ev = new EventAdd;
            emit sendRemoveSignal(/*checkVec,*/ (AddEv*)ev, Data_Map);
            if(ev){
                delete ev;
            }
        }
        break;
        case addType::Parm:
        {
            ParmAdd *par = new ParmAdd;
            emit sendRemoveSignal(/*checkVec,*/ (AddEv*)par, Data_Map);
            if(par){
                delete par;
            }
        }
        break;
        case addType::Agent:
        {
            AgentAdd *agent = new AgentAdd;
            emit sendRemoveSignal(/*checkVec,*/ (AddEv*)agent, Data_Map);
            if(agent){
                delete agent;
            }
        }
        break;
        default:
            break;
        }
    }
}

void AddPlatform::Init()
{
    this->setWindowTitle("Parse system");
    ui->plainTextEdit->setEnabled(false);
}
void AddPlatform::on_okBtn_clicked()
{
    this->hide();
//    qDebug() << Jsons.size();
    auto fu = ThreadPool::Instance().commit([this]() {
        start();
    });
    fu.get();
    emit sendParsefinished();
}
/*模拟-----
//        openxmlFile(SPC_PATH, spccontent);//jc
*/
